# coding:utf-8
from .data_collector import DataCollector
import json
import pandas as pd


class WeiboCrawler(DataCollector):
    uid = "2803301701"
    blog_limit_count = 50
    blog_items = []

    def __init__(self):
        DataCollector.__init__(self)
        self.algorithm_name = "微博内容"
        self.ipynb_template_name = "weibo_crawler-template.ipynb"

    def implent(self):
        DataCollector.implent(self)
        self.blog_items = []
        page = 1
        while True:
            url = "https://m.weibo.cn/api/container/getIndex?uid=%s&containerid=107603%s&page=%d" % (self.uid, self.uid, page)
            result = self.http.request(method="GET", url=url)
            json_data = json.loads(result.data.decode())
            if json_data["ok"] == 1:
                for card in json_data["data"]["cards"]:
                    mblog = card.get("mblog", None)
                    if mblog:
                        self.blog_items.append([mblog["id"], mblog["created_at"], mblog["text"].replace(r'<a href="/status', r'<a href="https://m.weibo.cn/status'), mblog["attitudes_count"], mblog["comments_count"], mblog["reposts_count"]])
                if len(self.blog_items) > self.blog_limit_count:
                    break
                else:
                    page = page + 1
                    continue
            else:
                break
        self.blog_items = self.blog_items[:self.blog_limit_count]
        self.df = pd.DataFrame(self.blog_items, columns=[
                               "ID", "发布时间", "内容(HTML格式)", "点赞数", "评论数", "转发数"])

    def prepareIpynbItems(self):
        DataCollector.prepareIpynbItems(self)
        self.ipynb_items["#blog_limit_count#"] = self.blog_limit_count
